/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.harvest.tei.svn;

import cz.mzk.oai.metadataRepository.harvest.tei.svnManager.Connector;
import cz.mzk.oai.metadataRepository.harvest.HarvestException;
import cz.mzk.oai.metadataRepository.harvest.tei.TeiHarvester;
import cz.mzk.oai.metadataRepository.metadata.DateStamp;
import cz.mzk.oai.metadataRepository.repository.Repository;
import java.io.File;
import java.util.List;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class TeiSvnHarvester extends TeiHarvester {

    private static final Logger logger = Logger.getLogger(TeiHarvester.class.getName());
    private final RecordHarvester recordHarvester;
    private final Connector svnConnector;

    public TeiSvnHarvester(Repository repository, String svnRepositoryUrl, File mappingFile) throws HarvestException {
        super(repository);
        try {
            svnConnector = new Connector(svnRepositoryUrl);
            SignatureSysnoMapper mapper = new SignatureSysnoMapper(mappingFile);
            recordHarvester = new RecordHarvester(repository, svnConnector, mapper);
        } catch (Exception e) {
            throw new HarvestException(e);
        }
    }

    public void harvestLatestRevision() throws HarvestException {
        try {
            LastRevisionHarvester harvester = new LastRevisionHarvester(svnConnector, recordHarvester);
            long revision = svnConnector.getLatestRevision();
            harvester.harvest(revision);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new HarvestException(ex);
        }
    }

    public void harvest(DateStamp from, DateStamp until) throws HarvestException {
        try {
            UpdatesHarvester harvester = new UpdatesHarvester(svnConnector, recordHarvester);
            List<Long> revisions = svnConnector.getSortedRevisions(from, until);
            for (Long revision : revisions) {
                harvester.harvest(revision);
                //break;  //TODO:remove
            }
        } catch (Exception ex) {
            throw new HarvestException(ex);
        }
    }
}
